package com.tchintech.pay.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tchintech.pay.R;
import com.tchintech.pay.delegate.OrderQueryDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.model.entity.OrderQueryModel;
import com.tchintech.pay.model.entity.OrderQueryResult;
import com.tchintech.pay.model.entity.ResultBean;
import com.tchintech.pay.net.HttpRequester;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.utils.OrderState;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/8 0008.
 */

public class OrderQueryActivity extends ActivityPresenter<OrderQueryDelegate> implements View.OnClickListener {
    OrderState orderState;
    private final String orderQueryURL = "Order/query";
    private Map<String,String> map;
    private int page = 1;
    private Handler handler;
    private Gson gson=new Gson();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setOnClickListener(this,R.id.bt_choose_config, R.id.bt_choose_next, R.id.bt_choose_front);
        handler();

    }

    private  void handler(){
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                String message=msg.obj.toString();
                 switch (msg.what){
                     case 1:
                         OrderQueryResult orderQueryResult = gson.fromJson(message,OrderQueryResult.class);
                         final List<OrderQueryModel> list = orderQueryResult.getList();
                         viewDelegate.getListview().setAdapter(new ArrayAdapter<OrderQueryModel>(OrderQueryActivity.this,
                                 R.layout.orderquery_listview, list) {

                             @NonNull
                             @Override
                             public View getView(int position, View convertView, ViewGroup parent) {
                                 View view = null;
                                 if (null != convertView) {
                                     view = convertView;
                                 } else {
                                     view = LayoutInflater.from(getContext()).inflate(R.layout.orderquery_listview, null);
                                 }
                                 OrderQueryModel orderQueryModel = list.get(position);
                                 TextView list_amount = (TextView) view.findViewById(R.id.list_amount);
                                 list_amount.setText("金额：" + orderQueryModel.getAmount());
                                 TextView list_time = (TextView) view.findViewById(R.id.list_time);
                                 list_time.setText("时间：" + orderQueryModel.getCreateTime());
                                 TextView list_state = (TextView) view.findViewById(R.id.list_state);
                                 list_state.setText(orderState.getState(orderQueryModel.getState()));
                                 return view;
                             }
                         });
                         break;
                     case 2:

                         break;
                 }
            }
        };


    }
    @Override
    protected void initData() {
        super.initData();
        orderState = new OrderState(this);
        map = new HashMap<>();
        map.put("page", page + "");
        httpPost(orderQueryURL,map, HttpCode.orderquery,null);
    }

  /*  private void httpPost(String url, Map<String, String> map, int what,File file) {
        String path = HttpRequester.path(url, map);
        try {
            showProgressDialog("", "加载中，请稍后...");
            HttpRequester.post(this, what, path, map, this, file);
        } catch (Exception e) {
            hideProgressDialog();
            showMsg("网络异常");
        }
    }*/
    @Override
    public void httpCallback(String resultBean, int what) {
        super.httpCallback(resultBean, what);

        Message msg=new Message();
        switch (what){
            case HttpCode.orderquery:
             /*   if(resultBean.getResCode().equals("1")){
                    msg.obj=resultBean.getResMessage();
                    msg.what=1;
                }else{
                    msg.obj=resultBean.getResMessage();
                    msg.what=2;
                }*/
                break;
        }
        handler.sendMessage(msg);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_choose_config:
                break;
            case R.id.bt_choose_next:

                try {
                    orderState = new OrderState(this);
                    map = new HashMap<>();
                    map.put("page", ++page + "");
                    httpPost(orderQueryURL,map, HttpCode.orderquery,null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt_choose_front:
                try {
                    orderState = new OrderState(this);
                    map = new HashMap<>();
                    map.put("page", (--page < 0 ? 0 : page) + "");
                    if (page < 0) {
                        page = 1;
                    }
                    httpPost(orderQueryURL,map, HttpCode.orderquery,null);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
        }
    }

    @Override
    protected Class<OrderQueryDelegate> getDelegateClass() {
        return OrderQueryDelegate.class;
    }
}
